package com.hr.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置
 */
@Configuration
//@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisConfig {
    //从配置文件中读取 集群节点
//    @Value("${spring.redis.cluster.nodes}")
//    private String[] nodes;
    //密码
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private Integer port;

    /**
     * 将redis连接工厂 注入到ioc容器中
     * @return
     */
    @Bean
    public JedisConnectionFactory connectionFactory() {
        /**
         * *******************************************
         * redis 连接工厂 集群代码 start
         */
//        //将其转化为 ArrayList
//        List<String> list = new ArrayList<String>(
//                Arrays.asList(nodes));
//        //集群配置
//        RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(list);
//        //密码
//        clusterConfig.setPassword(password);
//        //redis连接工厂
//        JedisConnectionFactory factory = new JedisConnectionFactory(clusterConfig);
        /**
         * redis 连接工厂 集群代码 end
         * *******************************************
         */
        //redis 连接工厂 单机代码
        //配置
        RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();
        standaloneConfig.setHostName(host);
        standaloneConfig.setPort(port);
        standaloneConfig.setPassword(password);
        //连接工厂对象
        JedisConnectionFactory factory = new JedisConnectionFactory(standaloneConfig);
        return factory;
    }

    /**
     * 将redis 操作类注入到 ioc容器中
     * @param connectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        //创建一个redis库的操作类
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
        //设置操作类的 连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);
        //配置存储数据时，序列化value时使用的 类
        GenericFastJsonRedisSerializer redisSerializer
                = new GenericFastJsonRedisSerializer();
        redisTemplate.setValueSerializer(redisSerializer);
        //配置存储、获取数据时，序列化key时使用的类
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //将查各项必要数据是否都已成功设置或初始化完毕
        redisTemplate.afterPropertiesSet();
        //返回此对象，注入到ioc容器中
        return redisTemplate;
    }
}